package com.lc.user.express.ordering;

import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lc.user.express.BaseFragment;
import com.lc.user.express.R;
import com.lc.user.express.adapter.HomeAdapter;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.httpserver.GetIndex;
import com.lc.user.express.model.HomeModel;
import com.lc.user.express.ordermanagement.OrderManagementFragment;
import com.lc.user.express.utils.LocationUtils;
import com.lc.user.express.utils.NetworkFlux;
import com.lc.user.express.view.pull.PullToRefreshBase;
import com.lc.user.express.view.pull.PullToRefreshListView;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import static com.lc.user.express.R.id.lv;
import static com.lc.user.express.ordermanagement.OrderManagementFragment.onRefreshOrderData;

/**
 * Created by Administrator on 2016/2/29. 下单
 */
public class OrderingFragment extends BaseFragment {
    public static OnCityChangedListener onCityChanged = null;//监听城市
    public static RefreshData onRefreshData = null;
    private TextView tv_city;//城市
    private TextView tv_car_count;//附近车辆数量
    private ImageView iv_top;//家居图片
//    private ListView lv;//车辆类型列表
    private HomeAdapter homeAdapter;//车辆类型列表adapter
    private List<HomeModel> list = new ArrayList<>();//车辆类型列表数据
    private String pot ="";
    private NetworkFlux networkFlux = new NetworkFlux() {
        @Override
        protected void onConnectChange(boolean isConnect) {
            if (isConnect) {
                getData(AppContext.myPreferences.getCityId());
            }
        }
    };
    private String point;

    private PullToRefreshListView pListView;

    @Override
    public View getContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment_ordering, container, false);
        setTitle("下单");
        setHeadLayoutVisibility(View.GONE);
        initView(v);
        if (!TextUtils.isEmpty(AppContext.myPreferences.getCityId()))
            getData(AppContext.myPreferences.getCityId());
        getActivity().registerReceiver(networkFlux, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        return v;
    }

    private void initView(View v) {
        onRefreshData = new RefreshData() {
            @Override
            public void onRefresh(String cityId) {
                getData(AppContext.myPreferences.getCityId());
            }
        };
        ((LinearLayout) v.findViewById(R.id.ll_city)).setOnClickListener(this);
        tv_city = (TextView) v.findViewById(R.id.tv_city);
        tv_car_count = (TextView) v.findViewById(R.id.tv_car_count);
        iv_top = (ImageView) v.findViewById(R.id.iv_top);
        iv_top.setOnClickListener(this);
       // lv = (ListView) v.findViewById(lv);

        tv_car_count.setOnClickListener(this);
        homeAdapter = new HomeAdapter(getActivity(), list);
      /*  lv.setAdapter(homeAdapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("cid", list.get(position).getId());
                bundle.putString("car_name", list.get(position).getCarName());
                show(OrderInfoCompleteActivity.class, bundle);
            }
        });*/
        onCityChanged = new OnCityChangedListener() {
            @Override
            public void onCityChanged(String city, String cityId) {
                tv_city.setText(TextUtils.isEmpty(city) ? "定位中..." : city);
                AppContext.myPreferences.setCity(city);
                AppContext.myPreferences.setCityId(cityId);
                AppContext.myPreferences.setIsShowCityDialog(false);
                getData(cityId);
            }
        };
        tv_city.setText(TextUtils.isEmpty(AppContext.myPreferences.getCity()) ? "定位中..." : AppContext.myPreferences.getCity());







        pListView = (PullToRefreshListView) v.findViewById(lv);
        pListView.setPullLoadEnabled(false);
        pListView.setScrollLoadEnabled(true);
        pListView.setPullRefreshEnabled(true);
        pListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                getData(AppContext.myPreferences.getCityId());
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getData(AppContext.myPreferences.getCityId());
            }
        });
        final ListView listView = pListView.getRefreshableView();
        listView.setDivider(null);
        listView.setDividerHeight(0);
        listView.setSelector(new ColorDrawable(Color.TRANSPARENT));
       // preferentialAdapter = new PreferentialAdapter(getActivity(), list);
        listView.setAdapter(homeAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              /*  Bundle bundle = new Bundle();
                bundle.putString("id",list.get(position).getId());
                bundle.putString("title",list.get(position).getTitle());
                show(ActivityWebActivity.class,bundle);*/
                Bundle bundle = new Bundle();
                bundle.putString("cid", list.get(position).getId());
                bundle.putString("car_name", list.get(position).getCarName());
                show(OrderInfoCompleteActivity.class, bundle);
            }

        });



        onRefreshOrderData = new OrderManagementFragment.OnRefreshOrderData() {
            @Override
            public void onRefresh() {
                getData(AppContext.myPreferences.getCityId());
            }
        };

    }

    public interface RefreshData {
        void onRefresh(String cityId);
    }
    private void getData(String cityId) {
        point = LocationUtils.getInstance().lat+","+ LocationUtils.getInstance().lng;
        if (!"0.0,0.0".equals(point)){
            AppContext.myPreferences.setPoint(point);
        }
        pot = AppContext.myPreferences.getPoint();
//        Toast.makeText(activity,"_"+ pot,Toast.LENGTH_LONG).show();
        new GetIndex(pot,cityId, new AsyCallBack<GetIndex.Info>() {
            @Override
            public void onSuccess(String toast, int type, GetIndex.Info info) throws Exception {
                tv_car_count.setText("您的附近有超过" + info.number + "辆货车可提供服务");
                list.clear();
                list.addAll(info.dataList);
                homeAdapter.updateListView(list);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);

                pListView.onPullDownRefreshComplete();
                pListView.onPullUpRefreshComplete();
            }
        }).execute(activity);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_top:
//                之前家居页面
//                show(FurnitureActivity.class);


                                show(JiaJuwxActivity.class);

                break;
            case R.id.ll_city:
                show(SelectCityActivity.class);
                break;
            case R.id.tv_car_count:
                show(NearbyCarsActivity.class);
                break;
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        onCityChanged = null;
        getActivity().unregisterReceiver(networkFlux);
    }

}
